package com.igorkundovic.guitarstab.pages;

import java.util.LinkedList;
import java.util.List;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.CleanupRender;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.igorkundovic.guitarstab.Artist;
import com.igorkundovic.guitarstab.Comment;
import com.igorkundovic.guitarstab.Tab;
import com.igorkundovic.guitarstab.User;
import com.igorkundovic.guitarstab.beans.ArtistBean;
import com.igorkundovic.guitarstab.beans.CommentBean;
import com.igorkundovic.guitarstab.beans.TabBean;
import com.igorkundovic.guitarstab.service.CommentService;
import com.igorkundovic.guitarstab.service.TabService;
import com.igorkundovic.guitarstab.service.UserService;

public class CommentSite {

	@ApplicationState
	@Property
	private User user;
	private boolean userExists;
	@Inject
	private CommentService commentservice;
	@Property
    private String message;
	@Property
	@Persist
	private String message2;
	@Property
	@Persist
	private List<Comment> comments;
	@Property
	private CommentBean commentbean;
	public Object onActivate(){
		if(userExists){
			return null;
		}
		return Welcome.class;
	}
	
	@SetupRender
	public void setUp(){
		comments=commentservice.retrieveAllComments();
        String messsage3=null;
		if(comments.isEmpty())
			messsage3="No one posted any comment! Be the first one!";
		
		message2=messsage3;
	}
	
	public Object onSuccess(){
		comments=null;
		CommentBean commentb=new CommentBean();
		commentb.setComment(message);
		commentb.setUser(user);
		commentservice.save(commentb);
		comments=commentservice.retrieveAllComments();
		return this;
	}
	 
	@CleanupRender
	public void cleanUp(){
		comments=null;
		message2=null;
		}
	
}
